package com.aline.ptrscr;

import java.io.*;
import java.util.Enumeration;
import java.util.Properties;

public class Config {

    private static Properties conf;

    public static final String RANDM_NAME = "随机名称", DATETIME_NAME = "当前年月日时分秒";
    private static final String FILE_NAME_RULE = "fileNameRule";
    private static final String TYPE = "type";
    private static final String CONF_FILE = "conf.properties";
    public static final String DEFAULT_PATH_KEY = "user.dir";
    private static final String SAVE_PATH = "savePath";
    private static final String AUTO_SAVE = "autoSave";

    static {
        /*
         *   autoSave=false
         *   savePath=
         *   fileNameRule=当前年月日时分秒
         *   type=png
         * */
        conf = new Properties();
        try {
            File file = new File(CONF_FILE);
            conf.load(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
            conf.setProperty(AUTO_SAVE, "false");
        }
    }

    static boolean isAutoSave() {
        try {
            return Boolean.valueOf(conf.getProperty(AUTO_SAVE));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    static String getSavePath() {
        String s = conf.getProperty(SAVE_PATH);
        if (s == null || s.trim().length() == 0) {
            return System.getProperty(DEFAULT_PATH_KEY);
        }
        return s;
    }

    static String getFileNameRule() {
        String s = conf.getProperty(FILE_NAME_RULE);
        if (s == null || s.trim().length() == 0) {
            return "当前年月日时分秒";
        }
        return s;
    }

    static String getType() {
        String s = conf.getProperty(TYPE);
        if (s == null || s.trim().length() == 0 || (!"png".equals(s) && !"jpg".equals(s) && !"jpeg".equals(s))) {
            return ".png";
        }
        return "." + s;
    }

    static void setType(String type) {
        conf.setProperty(TYPE, type);
    }

    static void setAutoSave(boolean v) {
        conf.setProperty(AUTO_SAVE, String.valueOf(v));
    }

    static void setSavePath(String path) {
        if (path == null || path.trim().length() == 0) {
            path = System.getProperty(DEFAULT_PATH_KEY);
        }
        conf.setProperty(SAVE_PATH, path);
    }

    static void setFileNameRule(String rule) {
        if (!RANDM_NAME.equals(rule)) {
            rule = DATETIME_NAME;
        }
        conf.setProperty(FILE_NAME_RULE, rule);
    }

    public static void saveToFile() {
        new Thread(() -> {
            synchronized (conf) {
                StringBuilder sb = new StringBuilder();
                Enumeration<?> enumeration = conf.propertyNames();
                File file = new File(CONF_FILE);
                try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"))) {
                    while (enumeration.hasMoreElements()) {
                        Object o = enumeration.nextElement();
                        sb.append(o);
                        sb.append("=");
                        sb.append(conf.getProperty(o.toString()).replaceAll("\\\\", "\\\\\\\\"));
                        sb.append("\n");
                    }
                    bw.write(sb.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

}
